﻿using System;
using System.Collections.Generic;
using System.Linq;
using IQToolkit;
using IQToolkit.Data;
using Infrastucture.Factories;
using TimeSplicerModule.DataModels;
using TimeSplicerModule.DomainModels;

namespace TimeSplicerModule.Factories
{
    public interface IProjectFactory : Infrastucture.Factories.IFactory<ProjectDataModel, ProjectModel>
    {
        List<ProjectModel> GetList(List<StatusModel> statuses, List<PriorityModel> priorities, List<UserModel> users);
    }

    public class ProjectFactory : Factory<ProjectDataModel, ProjectModel>, IProjectFactory
    {
        public ProjectFactory(DbEntityProvider provider, IEntityTable<ProjectDataModel> db)
            : base(provider, db)
        {

        }

        public List<ProjectModel> GetList(List<StatusModel> statuses, List<PriorityModel> priorities, List<UserModel> users)
        {
            var models = GetList();

            models.AsParallel().ForAll(x =>
                {
                    x.Status = statuses.FirstOrDefault(s => s.StatusGuid == x.StatusGuid);
                    x.Priority = priorities.FirstOrDefault(p => p.PriorityGuid == x.PriorityGuid);
                    x.ProjectManager = users.FirstOrDefault(u => u.UserGuid == x.ProjectManagerGuid);
                });

            return models;
        }
    }
}
